/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.logiclander.adhocracy.jira.cli;

import com.atlassian.jira.rest.client.JiraRestClient;
import com.atlassian.jira.rest.client.NullProgressMonitor;
import com.atlassian.jira.rest.client.domain.BasicIssue;
import com.atlassian.jira.rest.client.domain.Issue;
import com.atlassian.jira.rest.client.domain.SearchResult;
import com.logiclander.adhocracy.cli.CommandLineReactor;
import java.util.Iterator;

/**
 *
 * @author tim
 */
public class JiraListIssuesReactor implements CommandLineReactor {

    private boolean initialized = false;
    final SearchResult issueList;
    final JiraRestClient restClient;
    final NullProgressMonitor progressMonitor;

    public JiraListIssuesReactor(SearchResult sr, JiraRestClient jrc, NullProgressMonitor pm) {
        issueList=sr;
        restClient=jrc;
        progressMonitor=pm;
    }
    
    public void init() {
        initialized = true;
        System.out.println("----------------------------------------------------------------------");
        System.out.println("-- IssueList                                                        --");
        System.out.println("----------------------------------------------------------------------");
        System.out.println("-- Count: " + issueList.getTotal());
        System.out.println("----------------------------------------------------------------------");
    }

    public void process() {
        for (Iterator i = issueList.getIssues().iterator(); i.hasNext();) {
            BasicIssue bi = (BasicIssue) i.next();
            Issue expanded = restClient.getIssueClient().getIssue(bi.getKey(), progressMonitor);
            System.out.println("     key: " + expanded.getKey());
            System.out.println("    type: " + expanded.getIssueType().getName());
            System.out.println(" summary: " + expanded.getSummary());
            System.out.println("  status: " + expanded.getStatus().getName());
            System.out.println("reporter: " + expanded.getReporter().getName());
            if (expanded.getAssignee() != null) {
                System.out.println("assignee: " + expanded.getAssignee().getName());
            }
            if (expanded.getField("customfield_10320") != null) {
                System.out.println("    rank: " + expanded.getField("customfield_10320").getValue());
            }
            System.out.println("----------------------------------------------------------------------");
        }
    }

    public void destroy() {
        initialized = false;
    }
}
